#include <eventpp/eventpp.h>
using namespace eventpp;

struct echo_handler : public tcp_handler {
  virtual void on_accepted( tcp_connection * conn ) {
    printf( "client %p accepted.\n", conn );
  }

	virtual void on_read(tcp_connection * conn)
	{
        char buffer[1024] = {0};
        size_t read = conn->read(buffer, sizeof(buffer));
		// evbuffer *in = conn->get_input();
		printf("Received %zu bytes\n", read);
		printf("----- data ----\n");
		printf("%.*s\n", read, buffer);

		conn->write( buffer, read );

		evbuffer *out = conn->get_output();
		printf("writing %zu bytes\n", evbuffer_get_length(out));
	}

	virtual void on_write(tcp_connection * conn )
	{
		evbuffer *out = conn->get_output();
		printf("output buffer %zu bytes\n", evbuffer_get_length(out));
	}
  virtual void on_closed(  tcp_connection * conn )
  {
    printf( "client %p closed.\n", conn );
    conn->close();
  }
};


int main(int argc, char **argv)
{
	net_base base;

	tcp_acceptor_config config;
	config.port = 9000;
	echo_handler h;

	tcp_acceptor acceptor(base, &config, &h );
	acceptor.start();

	base.run();

	return 0;
}
